<?php
/**
 * Created by PhpStorm.
 * User: mj
 * Date: 17-5-19
 * Time: 下午6:09
 */

namespace App\Http\Controllers;


use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\Text;
use Illuminate\Support\Facades\Log;

class WechatController
{

    protected $app ;


    public function __construct()
    {
        $this->app = app()->wechat;
    }

    public function index()
    {
        
        $qrcode = $this->app->qrcode;
        $result = $qrcode->temporary(56, 6 * 24 * 3600);
        return view('index.qrcode' , compact('result','qrcode'));
    }


    /**
     * 验证成功
     */
    public function valid(){
        $server = $this->app->server;
        $server->setMessageHandler(function ($message) {
            switch ($message->MsgType) {
                case 'event':
                        return $this->handleEvent($message);
                    break;
                // ... 其它消息
                default:
                    $text = new Text(['content' => '呵呵']) ;
                    return $text ;
                    break;
            }
        });
        $response =  $this->app->server->serve();
        return $response->send();

    }

    /**
     * @param $message $message->FromUserName openId
     */
    private function handleEvent($message){
        switch($message->Event){
            case 'SCAN' :
                $text = new Text(['content' => '呵呵1']);
                return $text;
        }
    }

}